//
//  HomeTopTabView.m
//  万物记
//
//  Created by Ajen on 2019/6/14.
//  Copyright © 2019 Ajen. All rights reserved.
//

#import "HomeTopTabView.h"

const CGFloat kLabelRadius = 5;
const CGFloat kLabelSpace = 15;
const CGFloat kTextFontSize = 13;
const CGFloat kTextSelectedFontSize = 16;
const CGFloat kNormalLabelHeight = 30;
const CGFloat kSelectedLabelHeight = 35;


@implementation HomeTopTabView{
    UIView * labelBottomLine;
    NSMutableArray<UIView*> * tabSpaceRespondViewMuArray;//防止响应插入动作的View的数组
    NSInteger moveInInsertIndex;//在哪个间隙中移动
    NSInteger oldMoveInInsertIndex;//上一个手指停留的插入位置
    NSInteger endInInsertIndex;//在哪个间隙中结束
    UIImpactFeedbackGenerator *feedBackGenertor;//震动反馈
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        self.scrollView.contentSize = CGSizeMake(1500, 35);

        [self addSubview:self.scrollView];
        [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        self.scrollView.showsHorizontalScrollIndicator = FALSE;
    }
    return self;
}

-(void)setTitleArray:(NSArray *)titleArray{
    _titleArray = titleArray;
    [tabSpaceRespondViewMuArray removeAllObjects];
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if(_titleArray.count){
        NSMutableArray<UILabel *> * labelMuArray = [NSMutableArray arrayWithCapacity:0];
        tabSpaceRespondViewMuArray = [NSMutableArray arrayWithCapacity:0];
        
        for(int i = 0 ; i < _titleArray.count ; i ++){
            UILabel * label = [UILabel new];
            label.aj_Text([NSString stringWithFormat:@"  %@  ",_titleArray[i]]).aj_textColorHexStr(@"#132c33").aj_cornerRadius(kLabelRadius).aj_borderWidth(1).aj_borderColorHexStr(@"#132c33").aj_font([UIFont systemFontOfSize:kTextFontSize weight:UIFontWeightThin]).aj_addToView(self.scrollView);
            label.tag = 10000 + i;
            [label addTapBlock:^(id obj) {
                [self selectTabLabelWithTag:label.tag];
            }];
            
            UILongPressGestureRecognizer * longPgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
            longPgr.minimumPressDuration = 0.5;
            [label addGestureRecognizer:longPgr];
            
            if(i == 0){
                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(kNormalLabelHeight));
                    make.left.equalTo(self.scrollView).offset(20);
                    make.bottom.equalTo(self.mas_bottom).offset(kLabelRadius - 1);
                }];
            }else{
                UILabel * priorLabel = (UILabel *)[self viewWithTag:10000 + i - 1];//前一个label
                
                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(kNormalLabelHeight));
                    make.left.equalTo(priorLabel.mas_right).offset(kLabelSpace);
                    make.bottom.equalTo(self.mas_bottom).offset(kLabelRadius - 1);
                }];
            }
            
            if(i == 0){
                [label mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(kSelectedLabelHeight));
                }];
                label.font = [UIFont systemFontOfSize:kTextSelectedFontSize weight:UIFontWeightMedium];
                label.textColor = [UIColor colorWithAjenHex:0xfbb612 alpha:1];
            }
            
            //最后一次循环,获得最后一个视图尺寸,确认scrollveiw宽度
            if(i == _titleArray.count - 1){
                [label.superview layoutIfNeeded];
                CGFloat lastLabelRight = label.Right;
                if(lastLabelRight < [UIScreen mainScreen].bounds.size.width - 50){
                    lastLabelRight = [UIScreen mainScreen].bounds.size.width - 50;
                }
                self.scrollView.contentSize = CGSizeMake(lastLabelRight + 50, self.scrollView.contentSize.height);
            }
            
            [labelMuArray addObject:label];
        }
        
        //整体视图下面的线
        UIView * bottomLine = [UIView new];
        bottomLine.aj_bgColorHexStr(@"#132c33").aj_addToView(self.scrollView);
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.equalTo(self);
            make.height.equalTo(@1);
            make.width.equalTo(@(self.scrollView.contentSize.width));
        }];
        
        //选中的label上面覆盖的白线
        UILabel * priorLabel = (UILabel *)[self viewWithTag:10000];
        labelBottomLine = [UIView new];
        labelBottomLine.aj_bgColorHexStr(@"#ffffff").aj_addToView(self);
        [labelBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(priorLabel).offset(1);
            make.right.equalTo(priorLabel).offset(-1);
            make.height.equalTo(@1);
            make.bottom.equalTo(self);
        }];
        
        [self.scrollView layoutIfNeeded];
        
        //放置插入行为响应区域视图
        
        for(int i = 0 ; i < labelMuArray.count ; i++){
            if(i < labelMuArray.count - 1){
                if(i == 0){
                    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(labelMuArray[i].X - 120, labelMuArray[i].Y, 140, labelMuArray[i].Height)];
                    view.backgroundColor = [UIColor redColor];
                    view.alpha = 0.4;
                    view.hidden = self.respondView.hidden;
                    [self.scrollView addSubview:view];
                    [tabSpaceRespondViewMuArray addObject:view];
                }
                UIView * view = [[UIView alloc] initWithFrame:CGRectMake(labelMuArray[i].X + labelMuArray[i].Width * 0.7, labelMuArray[i].Y, labelMuArray[i].Width * 0.3 + kLabelSpace + labelMuArray[i + 1].Width * 0.3, labelMuArray[i].Height)];
                view.backgroundColor = [UIColor redColor];
                view.alpha = 0.4;
                view.hidden = self.respondView.hidden;
                [self.scrollView addSubview:view];
                [tabSpaceRespondViewMuArray addObject:view];
            }else{
                if(labelMuArray.count == 1){
                    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(labelMuArray[i].X - 120, labelMuArray[i].Y, 140, labelMuArray[i].Height)];
                    view.backgroundColor = [UIColor redColor];
                    view.alpha = 0.4;
                    view.hidden = self.respondView.hidden;
                    [self.scrollView addSubview:view];
                    [tabSpaceRespondViewMuArray addObject:view];
                }
                UIView * view = [[UIView alloc] initWithFrame:CGRectMake(labelMuArray[i].X + labelMuArray[i].Width * 0.7, labelMuArray[i].Y, 100 + labelMuArray[i].X + labelMuArray[i].Width * 0.7, labelMuArray[i].Height)];
                view.backgroundColor = [UIColor redColor];
                view.alpha = 0.4;
                view.hidden = self.respondView.hidden;
                [self.scrollView addSubview:view];
                [tabSpaceRespondViewMuArray addObject:view];
            }
        }
        
        for(int i = 0 ; i < tabSpaceRespondViewMuArray.count ; i++){
            tabSpaceRespondViewMuArray[i].hidden = YES;
        }
    }
    
//    UIView * red = [UIView new];
//    red.backgroundColor = [UIColor redColor];
//    [self addSubview:red];
//    [red mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.equalTo(@5);
//        make.center.equalTo(self);
//    }];
}

-(void)selectTabLabelWithTag:(NSInteger)tag{
    if(self.currentIndexBlock){
        self.currentIndexBlock(tag - 10000);
    }
    

    
    for(int i = 0 ; i < _titleArray.count ; i ++){
        UILabel * label = (UILabel *)[self viewWithTag:10000 + i];
        if( i == tag - 10000){
            [label mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(kSelectedLabelHeight));
            }];
            label.font = [UIFont systemFontOfSize:kTextSelectedFontSize weight:UIFontWeightMedium];
            label.textColor = [UIColor colorWithAjenHex:0xfbb612 alpha:1];
            [labelBottomLine mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(label).offset(1);
                make.right.equalTo(label).offset(-1);
                make.height.equalTo(@1);
                make.bottom.equalTo(self);
            }];
            
            //移动,将选中的放在中间
            if(self.scrollView.contentSize.width > self.scrollView.Width){
                if(label.CenterX > self.scrollView.Width / 2 && (self.scrollView.contentSize.width - 30) - label.CenterX > self.scrollView.Width / 2){
                    [self.scrollView setContentOffset:CGPointMake(label.CenterX - self.scrollView.CenterX - 10, 0) animated:YES];
                }else{
                    if((self.scrollView.contentSize.width - 30) - label.CenterX < self.scrollView.Width / 2){
                        [self.scrollView setContentOffset:CGPointMake((self.scrollView.contentSize.width - 30) - self.scrollView.Width, 0) animated:YES];
                    }
                    if(label.CenterX < self.scrollView.Width / 2){
                        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
                    }
                }
            }
            
            
            
        }else{
            [label mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(kNormalLabelHeight));
            }];
            label.font = [UIFont systemFontOfSize:kTextFontSize weight:UIFontWeightThin];
            label.aj_textColorHexStr(@"#132c33");
        }
    }
}

-(void)setTouchMovedPoint:(CGPoint)touchMovedPoint{
    CGFloat moveX = touchMovedPoint.x + self.scrollView.contentOffset.x;
    CGFloat moveY = touchMovedPoint.y + 20 - STATUS_BAR_HEIGHT - 60;//因为当前self视图在屏幕上是从Y=60开始的,所以如果换算成当前视图的坐标要-60
    //转换成当前视图坐标系的相对坐标点
    CGPoint currentMovePoint = CGPointMake(moveX, moveY);

    for(int i = 0 ; i < tabSpaceRespondViewMuArray.count ; i++){
        if(CGRectContainsPoint(tabSpaceRespondViewMuArray[i].frame, currentMovePoint)){
            moveInInsertIndex = i;
            if(oldMoveInInsertIndex != moveInInsertIndex){
                feedBackGenertor = nil;
            }
            if(!feedBackGenertor){
                feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
                [feedBackGenertor impactOccurred];
            }
        }
    }
    oldMoveInInsertIndex = moveInInsertIndex;
}

-(void)setTouchEndPoint:(CGPoint)touchEndPoint{
    feedBackGenertor = nil;
    CGFloat moveX = touchEndPoint.x + self.scrollView.contentOffset.x;
    CGFloat moveY = touchEndPoint.y + 20 - STATUS_BAR_HEIGHT - 60;//因为当前self视图在屏幕上是从Y=60开始的,所以如果换算成当前视图的坐标要-60
    //转换成当前视图坐标系的相对坐标点
    AJLog(@"%ld",tabSpaceRespondViewMuArray.count);
    CGPoint currentMovePoint = CGPointMake(moveX, moveY);
    for(int i = 0 ; i < tabSpaceRespondViewMuArray.count ; i++){
        if(CGRectContainsPoint(tabSpaceRespondViewMuArray[i].frame, currentMovePoint)){
            endInInsertIndex = i;
            if(self.insertBlock){
                self.insertBlock(i);
            }
        }
    }
}

-(void)setSelectedIndex:(NSInteger)selectedIndex{
    _selectedIndex = selectedIndex;
    [self selectTabLabelWithTag:10000 + _selectedIndex];
}

-(void)setOnlyShowIndex:(NSInteger)onlyShowIndex{
    _onlyShowIndex = onlyShowIndex;
    for(int i = 0 ; i < _titleArray.count ; i ++){
        UILabel * label = (UILabel *)[self viewWithTag:10000 + i];
        if( i == _onlyShowIndex){
            [label mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(kSelectedLabelHeight));
            }];
            label.font = [UIFont systemFontOfSize:kTextSelectedFontSize weight:UIFontWeightMedium];
            label.textColor = [UIColor colorWithAjenHex:0xfbb612 alpha:1];
            [labelBottomLine mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(label).offset(1);
                make.right.equalTo(label).offset(-1);
                make.height.equalTo(@1);
                make.bottom.equalTo(self);
            }];
            
            //移动,将选中的放在中间
            if(self.scrollView.contentSize.width > self.scrollView.Width){
                if(label.CenterX > self.scrollView.Width / 2 && (self.scrollView.contentSize.width - 30) - label.CenterX > self.scrollView.Width / 2){
                    [self.scrollView setContentOffset:CGPointMake(label.CenterX - self.scrollView.CenterX - 10, 0) animated:YES];
                }else{
                    if((self.scrollView.contentSize.width - 30) - label.CenterX < self.scrollView.Width / 2){
                        [self.scrollView setContentOffset:CGPointMake((self.scrollView.contentSize.width - 30) - self.scrollView.Width, 0) animated:YES];
                    }
                    if(label.CenterX < self.scrollView.Width / 2){
                        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
                    }
                }
            }
            
            
        }else{
            [label mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(kNormalLabelHeight));
            }];
            label.font = [UIFont systemFontOfSize:kTextFontSize weight:UIFontWeightThin];
            label.aj_textColorHexStr(@"#132c33");
        }
    }
    
}

-(void)moveToOut{
    moveInInsertIndex = 0;
    feedBackGenertor = nil;
}


-(void)longPress:(UILongPressGestureRecognizer *)longPgr{
    if(longPgr.state == UIGestureRecognizerStateBegan){
        if(self.renameIndexBlock){
            self.renameIndexBlock(longPgr.view.tag - 10000);
        }
    }
}
@end
